package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/system/sysUser")
@Tag(name = "用户管理模块")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/{current}/{size}")
    @Operation(summary = "分页带条件查询用户信息")
    @Parameters({@Parameter(name = "current", description = "当前页", required = true),
            @Parameter(name = "size", description = "页大小", required = true),
            @Parameter(name = "sysUserDto", description = "用户请求对象", required = false)})
    public Result<PageInfo> findByPage(@PathVariable Integer current,
                                       @PathVariable Integer size,
                                       SysUserDto sysUserDto) {
        PageInfo pageInfo = sysUserService.findByPage(current, size, sysUserDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("addUser")
    @Operation(summary = "添加用户信息方法")
    @Parameters(@Parameter(name = "sysUser", description = "用户对象", required = true))
    public Result addUser(@RequestBody SysUser sysUser) {
        sysUserService.addUser(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS).setMsg("添加成功");
    }

    @GetMapping("getById/{id}")
    @Operation(summary = "根据id查询用户信息")
    @Parameters(@Parameter(name = "id", description = "用户id", required = true))
    public Result<SysUser> getById(@PathVariable Long id) {
        SysUser sysUser = sysUserService.getById(id);
        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
    }

    @PutMapping("updateById")
    @Operation(summary = "根据id修改用户信息")
    @Parameters(@Parameter(name = "sysUser", description = "用户对象", required = true))
    public Result updateById(@RequestBody SysUser sysUser) {
        sysUserService.updateById(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS).setMsg("修改成功");
    }

    @DeleteMapping("deleteById/{id}")
    @Operation(summary = "根据id删除用户信息")
    @Parameters(@Parameter(name = "id", description = "用户id", required = true))
    public Result deleteById(@PathVariable Long id) {
        sysUserService.deleteById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS).setMsg("删除成功");
    }

}
